package com.detrum.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Switch;
import android.widget.TextView;

import com.detrum.base.R;
import com.detrum.bean.ShareBean;
import com.detrum.utils.LogUtils;

import java.util.List;

/**
 * Created by abc on 2018/3/15.
 */

public class ShareListAdatper extends BaseAdapter {
    private Context context;
    private List<ShareBean> list;
    private LayoutInflater layoutInflater;
    private ShareAdapterCall shareAdapterCall;

    public void setShareAdapterCall(ShareAdapterCall shareAdapterCall){
        this.shareAdapterCall = shareAdapterCall;
    }

    public interface  ShareAdapterCall{
        void isClick(int position ,boolean isCheck);
    }
    public ShareListAdatper(Context context, List<ShareBean> list) {
        this.context = context;
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ShareHolder holder;
        if(convertView==null){
            convertView = layoutInflater.inflate(R.layout.share_item,parent,false);
            holder = new ShareHolder();
            holder.app_icon = convertView.findViewById(R.id.app_icon);
            holder.app_name = convertView.findViewById(R.id.app_name);
            holder.app_select = convertView.findViewById(R.id.app_select);
            convertView.setTag(holder);
        }else{
            holder = (ShareHolder) convertView.getTag();
        }
        holder.app_select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    list.get(position).setSelect(1);
                }else{
                    list.get(position).setSelect(2);
                }
                LogUtils.i(list.get(position).getName()+"==========="+isChecked+"========"+list.get(position).getSelect());
                shareAdapterCall.isClick(position,isChecked);
            }
        });
        holder.app_name.setText(list.get(position).getName());
        holder.app_icon.setImageDrawable(list.get(position).getIcon());
        int select = list.get(position).getSelect();
        if(select==1){
            holder.app_select.setChecked(true);
        }else if(select==2){
            holder.app_select.setChecked(false);
        }
        LogUtils.i(list.get(position).getName()+"==========="+list.get(position).getSelect());
        return convertView;
    }

    class ShareHolder{
        ImageView app_icon;
        TextView app_name;
        Switch app_select;

    }
}
